package com.data.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.NoticeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import com.data.core.dao.NoticeMapper;
import com.data.core.entry.Notice;
import com.data.core.service.NoticeService;
import javax.annotation.Resource;
import java.util.Objects;

/**
 * @version: V1.0
 * @author: szw
 * @className: NoticeServiceImpl
 * @packageName: NoticeServiceImpl
 * @description: 公告接口实现
 * @data: 2021-1-21
 **/
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Resource
    private  NoticeMapper noticeMapper;


    /**
     * 保存公告接口实现
     * @param noticeVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-保存公告接口实现",operationType=BaseEnum.INSERT)
    public int save(NoticeVo noticeVo) throws Exception {
        if (Objects.isNull(noticeVo)){
            throw new BaseException(BaseEnum.NOTICE_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(noticeVo,true);
        }catch (Exception e){
            throw new BaseException(BaseEnum.SAVE_NOTICE_DATA_IS_ERROR);
        }
    }

    /**
     * 更新公告接口实现
     * @param noticeVo
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-更新公告接口实现",operationType=BaseEnum.UPDATE)
    public int update(NoticeVo noticeVo) throws Exception {
        if (Objects.isNull(noticeVo)){
            throw new BaseException(BaseEnum.NOTICE_DATA_IS_NULL);
        }
        try {
            return saveToUpdate(noticeVo,false);
        }catch (Exception e){
            throw new BaseException(BaseEnum.UPDATE_NOTICE_DATA_IS_ERROR);
        }
    }

    /**
     * 查询公告接口实现
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @ApiLogDetail(detail = "核心服务-查询公告接口实现",operationType=BaseEnum.SELECT)
    public Notice findNoticeById(Long id) throws Exception {
        if (null==id){
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            return noticeMapper.selectById(id);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_NOTICE_DATA_IS_ERROR);
        }
    }

    /**
     * 删除公告接口实现
     * @param id
     * @return
     * @throws Exception
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-删除公告接口实现",operationType=BaseEnum.DELETE)
    public int delete(Long id) throws Exception {
        if (null==id){
            throw new BaseException(BaseEnum.ID_IS_NULL);
        }
        try {
            return noticeMapper.deleteById(id);
        }catch (Exception e){
            throw new BaseException(BaseEnum.DELETE_NOTICE_DATA_IS_ERROR);
        }
    }

    /**
     * 分页查询公告接口实现
     * @param page
     * @param noticeVo
     * @return
     * @throws Exception
     */
    @Override
    @ApiLogDetail(detail = "核心服务-分页查询公告接口实现",operationType=BaseEnum.SELECT)
    public IPage<Notice> noticePage(Page<Notice> page, NoticeVo noticeVo) throws Exception {
        if (Objects.isNull(noticeVo)){
            throw new BaseException(BaseEnum.PROBLEM_DATA_IS_NULL);
        }
        try {
            QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
            return  noticeMapper.selectPage(page,queryWrapper);
        }catch (Exception e){
            throw new BaseException(BaseEnum.FIND_NOTICE_DATA_IS_ERROR);
        }
    }

    /**
     * 更新、保存公告
     * @param noticeVo
     * @param flag true 保存 false 更新
     * @return
     */
    private int saveToUpdate(NoticeVo noticeVo, Boolean flag){
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeVo,notice);
        if (flag){
            return noticeMapper.insert(notice);
        }
        return noticeMapper.updateById(notice);
    }
}
